package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.controller.dto.TrainClassDTO;
import com.yqg.education.entity.ExamSign;
import com.yqg.education.mapper.ExamSignMapper;
import com.yqg.education.service.IExamSignService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ExamSignController 前端控制器
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/exam-sign")
public class ExamSignController {

    @Resource
    private IExamSignService examSignService;
    @Resource
    private ExamSignMapper examSignMapper;
    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody ExamSign examSign) {
        examSign.setCreateTime(LocalDateTime.now());
        LambdaQueryWrapper<ExamSign> examSignLambdaQueryWrapper = new LambdaQueryWrapper<>();
        examSignLambdaQueryWrapper.eq(ExamSign::getCourseId, examSign.getCourseId());
        examSignLambdaQueryWrapper.eq(ExamSign::getStuId, examSign.getStuId());
        return Result.success(examSignService.saveOrUpdate(examSign,examSignLambdaQueryWrapper));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        ExamSign examSign =examSignService.getById(id);
        return Result.success(examSignService.saveOrUpdate(examSign));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ExamSign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<ExamSign> list=  examSignService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(examSignService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                            String username,
                           String courseName,
                           String examName) {
        pageNum = pageNum * pageSize - pageSize;
        List<ExamSign> trainClassList = examSignMapper.pageList(pageNum, pageSize,username,courseName,examName);
        Long count = examSignMapper.findCount(username,courseName,examName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", trainClassList);
        map.put("total", count);
        return Result.success(map);
    }
    @GetMapping("/stuPage")
    public Result stuFindPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String search,
                           @RequestParam Integer stuId) {
        pageNum = pageNum * pageSize - pageSize;
        List<ExamSign> trainClassList = examSignMapper.stuPageList(pageNum, pageSize,search,stuId);
        Long count = examSignMapper.stuFindCount(search,stuId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", trainClassList);
        map.put("total", count);
        return Result.success(map);
    }
    /**
     * 学员端查询是否已学习接口
     */
    @GetMapping("/stuFindIsLearned")
    public Result stuFindIsLearned(@RequestParam Integer courseId,
                                     @RequestParam Integer stuId){
        Map<String,Object> map= examSignService.stuFindIsLearned(courseId,stuId);
        return Result.success(map);
    }


}

